package com.stock.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stock.dao.IPointUsageRecordDao;
import com.stock.entity.PointUsageRecordEntity;
import com.stock.service.IPointUsageRecordService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PointUsageRecordService extends ServiceImpl<IPointUsageRecordDao, PointUsageRecordEntity> implements IPointUsageRecordService {

    /**
     * 判断用户今日是否已经使用过积分
     * @param username
     * @return
     */
    @Override
    public boolean hasUsedToday(String username) {
        QueryWrapper<PointUsageRecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                .eq("bill_date", DateUtil.today())
                .eq("is_del",0);
        return this.count(queryWrapper) > 0;
    }

    /**
     * 添加积分使用记录
     * @param username
     * @return
     */
    @Override
    public boolean addUsageRecord(String username) {
        PointUsageRecordEntity record = new PointUsageRecordEntity();
        record.setUsername(username);
        record.setBillDate(new Date());
        return this.save(record);
    }

}
